package com.tjbank.cssys.framework.base.api;

import com.tjbank.cssys.framework.base.support.MultiDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.Date;

/**
 * 基础控制类，提供分页数据封装，结果返回功能
 *
 * @author guzheng@tansun.com.cn
 * @date 2018-1-3
 */
@CrossOrigin
@SuppressWarnings("all")
public class BaseController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new MultiDateFormat(), true));
    }

    /**
     * 构建成功结果
     *
     * @param msg
     * @param data
     * @return
     */
    public Response success(String msg, Object data) {
        return Response.success(ResultCodeEnum.SUCCESS, msg, data);
    }

    /**
     * 构建失败结果
     *
     * @param msg
     * @param data
     * @return
     */
    public Response failure(String msg, Object data) {
        return Response.failure(msg, data);
    }

    /**
     * 构建失败结果
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public Response failure(Integer code, String msg, Object data) {
        return Response.failure(code, msg, data);
    }

    /**
     * 构建失败结果
     *
     * @param code
     * @param data
     * @return
     */
    public Response failure(Integer code, Object data) {
        return Response.failure(code, null, data);
    }

    /**
     * 构建失败结果
     *
     * @param code
     * @return
     */
    public Response failure(Integer code) {
        return Response.failure(code, null, null);
    }

    /**
     * 构建成功结果
     *
     * @return
     */
    public Response success() {
        return success(null, null);
    }

    /**
     * 构建成功结果带信息
     *
     * @param msg
     * @return
     */
    public Response success(String msg) {
        return success(msg, null);
    }

    /**
     * 构建成功结果待数据
     *
     * @param data
     * @return
     */
    public Response success(Object data) {
        return success(null, data);
    }

    /**
     * 构建失败结果
     *
     * @return
     */
    public Response failure() {
        return failure(0, null, null);
    }

    /**
     * 构建失败结果待数据
     *
     * @param msg
     * @return
     */
    public Response failure(String msg) {
        return failure(msg, null);
    }

    /**
     * 构建失败结果带数据
     *
     * @param data
     * @return
     */
    public Response failure(Object data) {
        return failure(0, null, data);
    }


}
